package com.example.demo;

import com.example.demo.entity.BookFetcher;
import com.example.demo.entity.BookStore;
import com.example.demo.entity.BookStoreFetcher;


import com.example.demo.entity.dto.BookStoreInput;
import org.babyfish.jimmer.client.FetchBy;
import org.babyfish.jimmer.sql.JSqlClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
public class IndexController {

    @Autowired
    private JSqlClient sqlClient;

    public static final BookStoreFetcher bookStoreFetcher = BookStoreFetcher.$.allScalarFields()
            .books(
                    BookFetcher.$.allScalarFields()
            );


    @PostMapping("saveBookStore")
    @Transactional
    public void saveBookStore(@RequestBody BookStoreInput bookStoreInput){
        sqlClient.save(bookStoreInput);
    }

    @GetMapping("findBookStoreById")
    public @FetchBy("bookStoreFetcher") BookStore findBookStoreById(@RequestParam Long id){
        return sqlClient.findById(bookStoreFetcher, id);
    }
}
